var base64 = new Base64();
var vm = new Vue({
    el: '#app',
    data() {
        return {
            orderList: [],
            curData: {
                cosmetologyMoney: 0,
                year: 0,
                receiptQuantity: 0,
                cosmetologyQuantity: 0,
                complaintQuantity: 0,
                carWashQuantity: 0,
                negativeCommentQuantity: 0,
                reviewQuantity: 0,
                receiptTime: 0,
                month: 0,
                due: 0,
                carWashMoney: 0,
                praiseQuantity: 0
            },
            orderStatus: {
                PENDING: 0,
                CHECK_PENDING: 1,
                UNACCEPTED: 2,
                ACCEPTED: 3,
                TRADE_SUCCESS: 4,
                TRADE_CLOSE: 5,
                WAIT_FOR_SERVICE: 6
            },
            curYear: 0,
            curMonth: 0
        }
    },
    methods: {
        init: function() {
            var date = new Date;
            vm.curYear = date.getFullYear();
            vm.curMonth = date.getMonth() + 1;

            var params = new Object();
            params.technician = api.getGlobalData({
                key: 'technicianid'
            });
            params.year = vm.curYear;
            params.month = vm.curMonth >= 10 ? vm.curMonth : '0' + vm.curMonth;
            params.day = '';
            params.groupBy = 'month'
            vm.getCurMonthData(params);
            //隐藏加载页面
            api.hideProgress();

        },
        hasLogin: function() {
            var technicianid = api.getGlobalData({
                key: 'technicianid'
            });
            if (technicianid != '') {
               // callback();
               vm.init();
            } else {
                //隐藏加载页面
                api.hideProgress();
                api.setFrameGroupIndex({
                    name: 'IndexGroup',
                    index: 2,
                    reload: true
                });
            }
        },
        hasAcceptedOrderList: function(callback) {
            var params = {}
            params.technician = api.getGlobalData({
                key: 'technicianid'
            });
            params.status = this.orderStatus.ACCEPTED + ',' + this.orderStatus.WAIT_FOR_SERVICE;
            params.asc = true;
            $api.ajax(function(ret, err) {
                if (ret.length > 0) {
                    alert('您还有进行中的订单，请先处理进行中的订单。');
                    api.setFrameGroupIndex({
                        name: 'IndexGroup',
                        index: 3,
                        reload: true
                    });
                } else {
                    callback();
                }
            }, 'order/findOrderList', 'post', params);
        },
        previousMonth: function() {
            var curMonth = vm.curMonth;
            vm.curMonth = (curMonth - 1) == 0 ? 12 : (curMonth - 1);
            if (curMonth == 1) {
                vm.curYear = vm.curYear - 1;
            }

            var params = new Object();
            params.technician = api.getGlobalData({
                key: 'technicianid'
            });
            params.year = vm.curYear;
            params.month = vm.curMonth >= 10 ? vm.curMonth : '0' + vm.curMonth;
            params.day = '';
            params.groupBy = 'month'
            if (api.getGlobalData({
                    key: 'curYear'
                }) == vm.curYear && api.getGlobalData({
                    key: 'curMonth'
                }) == vm.curMonth) {
                vm.getCurMonthData(params);
            } else {
                vm.getData(params);
            }
        },
        nextMonth: function() {
            var curMonth = vm.curMonth;
            vm.curMonth = (curMonth + 1) == 13 ? 1 : (curMonth + 1);
            if (curMonth == 12) {
                vm.curYear = vm.curYear + 1;
            }

            var params = new Object();
            params.technician = api.getGlobalData({
                key: 'technicianid'
            });
            params.year = vm.curYear;
            params.month = vm.curMonth >= 10 ? vm.curMonth : '0' + vm.curMonth;
            params.day = '';
            params.groupBy = 'month'
            console.log('=============================' + vm.curMonth);
            if (api.getGlobalData({
                    key: 'curYear'
                }) == vm.curYear && api.getGlobalData({
                    key: 'curMonth'
                }) == vm.curMonth) {
                vm.getCurMonthData(params);
            } else {
                vm.getData(params);
            }
        },
        getCurMonthData: function(params) {
            vm.getImmediateOrders(params);
            params.status = vm.orderStatus.TRADE_SUCCESS;
            params.type = 'hadPhoto';
            vm.findOrderList(params);
        },
        getData: function(params) {
            vm.getOrders(params);
            params.status = vm.orderStatus.TRADE_SUCCESS;
            params.type = 'hadPhoto';
            vm.findOrderList(params);
        },
        getImmediateOrders: function(params) {
            $api.ajax(function(ret, err) {
                console.log('ret: ' + JSON.stringify(ret));
                if (ret) {
                    vm.curData = ret;
                } else {
                    vm.curData = {
                        cosmetologyMoney: 0,
                        year: vm.curYear,
                        receiptQuantity: 0,
                        cosmetologyQuantity: 0,
                        complaintQuantity: 0,
                        carWashQuantity: 0,
                        negativeCommentQuantity: 0,
                        reviewQuantity: 0,
                        receiptTime: 0,
                        month: vm.curMonth,
                        due: 0,
                        carWashMoney: 0,
                        praiseQuantity: 0
                    }
                }
            }, 'v1/technicianExpand/getImmediateTechnicianData', 'post', params);
        },
        getOrders: function(params) {
            console.log('params: ' + JSON.stringify(params));
            $api.ajax(function(ret, err) {
                if (ret && ret.length != 0) {
                    vm.curData = ret[0];
                } else {
                    vm.curData = {
                        cosmetologyMoney: 0,
                        year: vm.curYear,
                        receiptQuantity: 0,
                        cosmetologyQuantity: 0,
                        complaintQuantity: 0,
                        carWashQuantity: 0,
                        negativeCommentQuantity: 0,
                        reviewQuantity: 0,
                        receiptTime: 0,
                        month: vm.curMonth,
                        due: 0,
                        carWashMoney: 0,
                        praiseQuantity: 0
                    }
                }
            }, 'v1/technicianExpand/getTechnicianDayExpand', 'post', params);
        },
        findOrderList: function(params) {
            $api.ajax(function(ret, err) {
                console.log('=============ret' + ret.length);
                vm.orderList = ret;
            }, 'order/findOrderList', 'post', params);
        }
    }
});
